﻿#include "itemedit.h"
#include "ui_itemedit.h"

ItemEdit::ItemEdit(QWidget *parent, const QString &table) :
    BaseEditorDialog(parent),
    _table(table),
    ui(new Ui::ItemEdit)
{
    ui->setupUi(this);

    view=nullptr;
    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);

    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QTableView::NoEditTriggers);
    ui->tableView->resizeColumnToContents(0);

    updateView();
    createMenu();
}

ItemEdit::~ItemEdit()
{
    delete ui;
}

void ItemEdit::closeEvent(QCloseEvent *)
{
    this->accept();
}

void ItemEdit::updateView()
{
    model->setTable(_table);
    model->select();

    model->setHeaderData(0,Qt::Horizontal,QStringLiteral("按键"));    //key
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("标签"));    //label
    ui->tableView->hideColumn(2);
}

void ItemEdit::generateMenu()
{
    menu->addAction(QStringLiteral("添加..."),this,&ItemEdit::newItem);
    menu->addAction(QStringLiteral("编辑..."),this,&ItemEdit::editItem);
    menu->addAction(QStringLiteral("更新"),this,&ItemEdit::updateView);
    menu->addSeparator();
    menu->addAction(QStringLiteral("撤销"),this,&ItemEdit::revert);
    menu->addAction(QStringLiteral("保存"),this,&ItemEdit::save);
    menu->addSeparator();
    menu->addAction(QStringLiteral("删除"),this,&ItemEdit::removeItem);
}

void ItemEdit::newItem()
{
    ItemAdd itemAdd(this,_table);
    itemAdd.setWindowTitle(QStringLiteral("新条目"));

    if(itemAdd.exec() == QDialog::Accepted)
        updateView();
}

void ItemEdit::editItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(!index.isValid())
        return;

    QString key=index.sibling(index.row(),0).data().toString();
    QString label=index.sibling(index.row(),1).data().toString();
    if(key.isEmpty() || label.isEmpty())
        return;

    QSqlQuery query;
    query.exec(QString("select detail from %1 where key='%2'").arg(_table).arg(key));
    if(query.next())
    {
        QString detail=query.value("detail").toString();
        ItemAdd itemAdd(this,_table);
        itemAdd.setWindowTitle(QStringLiteral("编辑条目"));

        itemAdd.setKey(key);
        itemAdd.setLabel(label);
        itemAdd.setDetail(detail);
        itemAdd.triggerEditMode();
        if(itemAdd.exec() == QDialog::Accepted)
            updateView();
    }
}

void ItemEdit::removeItem()
{
    if(ui->tableView->currentIndex().isValid())
    {
        int curRow = ui->tableView->currentIndex().row();
        model->removeRow(curRow);
    }
}

void ItemEdit::on_tableView_clicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        QString detail=index.sibling(index.row(),2).data().toString();
        if(detail.isEmpty())
            return;
        display(detail);
    }
}

void ItemEdit::display(const QString &detail)
{
    if(view==nullptr)
        view=new ItemView(this);

    view->move(this->x() + this->width() + 20,this->y());
    view->display(detail);
}
